package com.example.keshe0337.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


import com.example.keshe0337.R;
import com.example.keshe0337.biz.SpaceService;
import com.example.keshe0337.biz.UserService;
import com.example.keshe0337.entity.ParkingSpace;
import com.example.keshe0337.entity.User;
import com.example.keshe0337.ui.fragment.AllParkingFragment;
import com.example.keshe0337.ui.fragment.MyParkingFragment;
import com.example.keshe0337.ui.fragment.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        SpaceService spaceService = new SpaceService(this);

//        UserService userService = new UserService(this);
//        User user = new User();
//        user.setAge(20);
//        user.setName("张三");
//        user.setPhone("13812345678");
//        user.setPassword("123456");
//        userService.register(user);
//
//
        ParkingSpace parkingSpace = new ParkingSpace();
        parkingSpace.setSpaceNum("A102");
        parkingSpace.setLocation("地下一层A区");
        parkingSpace.setType("小型车位");
        parkingSpace.setHourlyFee(20.0);
        spaceService.addParkingSpace(parkingSpace);

        parkingSpace.setSpaceNum("A102");
        parkingSpace.setLocation("地下一层A区");
        parkingSpace.setType("小型车位");
        parkingSpace.setHourlyFee(20.0);
        spaceService.addParkingSpace(parkingSpace);

        parkingSpace.setSpaceNum("A103");
        parkingSpace.setLocation("地下一层A区");
        parkingSpace.setType("大型车位");
        parkingSpace.setHourlyFee(20.0);
        spaceService.addParkingSpace(parkingSpace);

        parkingSpace.setSpaceNum("B102");
        parkingSpace.setLocation("地下一层B区");
        parkingSpace.setType("中型车位");
        parkingSpace.setHourlyFee(20.0);
        spaceService.addParkingSpace(parkingSpace);

        List<ParkingSpace> allPSs = spaceService.getAllPSs();
        for (ParkingSpace p :allPSs) {
            Log.d("TEXT2",p.getSpaceNum());
        }

        replaceFragment(new AllParkingFragment());

        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();

            if (itemId == R.id.nav_my_parking) {
                replaceFragment(new MyParkingFragment());
                Log.d("MENU", "点击了我的停车位");
                return true;
            } else if (itemId == R.id.nav_all_parking) {
                replaceFragment(new AllParkingFragment());
                Log.d("MENU", "点击了全部停车位");
                return true;
            } else if (itemId == R.id.nav_profile) {
                replaceFragment(new ProfileFragment());
                Log.d("MENU", "点击了我的");
                return true;
            }
            return false;
        });

    }

    private void replaceFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

}
